package com.mazaiting.emotion.adapter

import android.content.Context
import android.util.ArrayMap
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.BaseAdapter
import android.widget.ImageView
import androidx.fragment.app.FragmentActivity
import com.mazaiting.emotion.R

class EmotionGridViewAdapter(
    activity: FragmentActivity,
    emotionNames: MutableList<String>,
    itemWidth: Int,
    map: ArrayMap<String, Int>
) : BaseAdapter() {
    private val context: Context = activity
    private val emotionNames: List<String> = emotionNames
    private val itemWidth: Int = itemWidth
    private val map: ArrayMap<String, Int> = map

    override fun getItem(position: Int): String = this.emotionNames[position]

    override fun getItemId(position: Int): Long = position.toLong()

    /**
     * +1 最后一个为删除按钮
     */
    override fun getCount(): Int = this.emotionNames.size + 1


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val emotion = ImageView(context)
        // 设置内边距
        emotion.setPadding(itemWidth / 8, itemWidth / 8, itemWidth / 8, itemWidth / 8)
        val params = AbsListView.LayoutParams(itemWidth, itemWidth)
        emotion.layoutParams = params

        //判断是否为最后一个item
        if (position == count - 1) {
            emotion.setImageResource(R.drawable.compose_emotion_delete)
        } else {
            val emotionName = emotionNames[position]
//            iv_emotion.setImageResource(EmotionUtils.getImgByName(emotion_map_type, emotionName))
            map[emotionName]?.let { emotion.setImageResource(it) }
        }

        return emotion
    }
}
